package com.example.springboot.utils;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/5/25
 * @Time: 13:06
 * @Email: 904593011@qq.com
 * @Description:
 **/
@Component
public class DateUtils {
    public static Date convert(String source) {
        if (source == null || "".equals(source)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = null;
        Date date = null;
        try {
            // 对yyyy-MM-dd HH:mm:ss的日期格式进行处理
            if (source.contains("-")) {
                switch (source.length()) {
                    case 10:
                        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        break;
                    case 19:
                        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                }
            } else if (source.contains("/")) {
                switch (source.length()) {
                    case 10:
                        simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                        break;
                    case 19:
                        simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                }
            } else {
                return null;
            }
            date = simpleDateFormat.parse(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
}
